var last_image = {
    data_path: '',
    data_src: '',
    short_path: ''
};

function init(){
    imgPreview();
    if($('.item-list').length > 0){
        showPanelUpdate('.item-list', getUpdateRow);
        submitUpdate(updateRow);
        changeStatus();
    } else {
        //save last image
        var ss = $('#a-input-category-icon img').attr('src');
        last_image.data_path = ss;
        last_image.data_src = $('#a-input-category-icon img').attr('data-src');
        last_image.short_path  = $('#input-category-icon').length>0?$('#input-category-icon').val():"";

        setCategoryIcon();
        submitUpdateForm('.btn-submit-update', evtAfterCategoryForm);
        uploadImage($('#frm-upload-img'));
    }
}

function evtAfterCategoryForm(data){
    if(data.code==1 && data.href!==undefined){
        if(document.URL != data.href){
            window.location = data.href;
        }
    }
}

function getUpdateRow(row){
    var html = row.parents('table').find('tr.tr-temp').clone().removeClass('hidden').removeClass('tr-temp').addClass('tr-update-temp');
    html.find('#input-category-title').val(row.find('.input-category-title').text().trim());
    html.find('#input-category-slug').val(row.find('.input-category-slug').attr('data-slug').trim());
    html.find('#input-category-order').val(row.find('.input-category-order').html().trim());
    html.find('#input-category-delete-flg').prop('checked', row.find('.input-category-delete-flg .checkbox').is(':checked'));
    html.find('#input-category-id').val(row.attr('data-id'));
    return html;
}

function changeStatus(){
    $('td.input-category-delete-flg input.checkbox').change(function(){
        var url = $(this).parents('table').attr('data-href');
        var value = $(this).is(':checked')==true?1:0;
        var name = $(this).attr('data-name');
        var category = {id: $(this).parents('tr').attr('data-id')};
        category[name] = value;
        changeState(url, {category: category});
    });
}

function updateRow(data){
    var trEditting = $('tr.editting');
    var trTempUpdate = $('tr.tr-update-temp');
    trEditting.find('.input-category-title').text(trTempUpdate.find('#input-category-title').val());
    trEditting.find('.input-category-title').attr('href', data.href);
    trEditting.find('.input-category-slug').attr('href', data.front_href);
    trEditting.find('.input-category-slug').attr('data-slug', trTempUpdate.find('#input-category-slug').val().trim());
    trEditting.find('td.input-category-order').html(trTempUpdate.find('#input-category-order').val());
    trEditting.find('td.input-category-delete-flg input').prop('checked', trTempUpdate.find('#input-category-delete-flg').is(':checked'));
}

function setCategoryIcon(){
    $('.img-block-edit').click(function(e){
        e.preventDefault();
        var folder      = $(this).attr('data-folder');
        var file_name   = $(this).attr('data-name');
        var file_path   = $(this).find('img.media-object').attr('src');
        $('#input-category-icon').val(folder + file_name);
        $('#a-input-category-icon img').attr('src', file_path).removeAttr('data-src').removeAttr('style');
        return false;
    });

    $('.btn-choose-image').click(function(){

    });

    $('.btn-close-image').click(function(){
        $('#a-input-category-icon img').attr('data-src', last_image.data_src);
        $('#a-input-category-icon img').attr('src', last_image.data_path);
        $('#input-category-icon').val(last_image.short_path);
    });
}

function resetModalChooseImage(modal){
    $(modal).modal('hide');
}